/**
 *
 * 2007-11-3 上午10:00:38
 * @author xuhuan
 */

package edu.ccut.saturn.studio.logic.model.xml.loader;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Point;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import edu.ccut.saturn.studio.logic.model.Shape;
import edu.ccut.saturn.studio.logic.model.saturn.SaturnDecisionNode;
import edu.ccut.saturn.studio.logic.model.saturn.SaturnParameter;

public class DecisionLoader extends NodeLoader {

	private static final String DECISION_LOADER = "";
	
	protected SaturnDecisionNode createSaturnDecisionNode(){
		return new SaturnDecisionNode();
	}

	@Override
	public List<Shape> loadNodes(Element root) {
		NodeList decisionNodes = root
				.getElementsByTagName(SaturnDecisionNode.KEY_NODE_DECISION);
		List<Shape> shapes = new ArrayList<Shape>(decisionNodes.getLength());
		for (int i = 0; i < decisionNodes.getLength(); i++) {
			String thenid = DECISION_LOADER, elseid = DECISION_LOADER, expr = DECISION_LOADER;
			SaturnDecisionNode saturnDecisionNode = createSaturnDecisionNode();

			Element decisionNode = (Element) decisionNodes.item(i);

			String shapeId = super.getNodeAttributeValue(decisionNode,
					Shape.KEY_ATTRIBUTE_SHAPE_ID);
			String nextId = super.getNodeAttributeValue(decisionNode,
					Shape.KEY_ATTRIBUTE_SHAPE_NEXT_ID);

			NodeList childNodes = decisionNode
					.getElementsByTagName(SaturnDecisionNode.SHOW_DECISION_NODE_IF);

			for (int j = 0; j < childNodes.getLength(); j++) {
				Node ifNode = childNodes.item(j);
				thenid = super.getNodeAttributeValue(ifNode,
						SaturnDecisionNode.SHOW_DECISION_NODE_IF_ATTRIBUTE_THEN);
				elseid = super.getNodeAttributeValue(ifNode,
						SaturnDecisionNode.SHOW_DECISION_NODE_IF_ATTRIBUTE_ELSE);
				expr = super.getNodeElementValue(ifNode,
						SaturnDecisionNode.SHOW_DECISION_NODE_IF_EXPR);
			}

			String name = super.getNodeElementValue(decisionNode,
					SaturnDecisionNode.SHOW_DECISION_NODE_NAME);
			String description = super.getNodeElementValue(decisionNode,
					SaturnDecisionNode.SHOW_DECISION_NODE_DESCRITPION);
			List<SaturnParameter> parameters = super
			.getParameters(decisionNode);
			Point location = super.getLocation(decisionNode);
			Dimension size = new Dimension(50, 70);
				
			saturnDecisionNode.setNode(decisionNode);
			saturnDecisionNode.setName(name);
			saturnDecisionNode.setDescription(description);
			saturnDecisionNode.setId(shapeId);
			saturnDecisionNode.setNextId(nextId);
			saturnDecisionNode.setThenid(thenid);
			saturnDecisionNode.setFalseId(elseid);
			saturnDecisionNode.setExpression(expr);
			saturnDecisionNode.setLocation(location);
			saturnDecisionNode.setSize(size);
			saturnDecisionNode.setParameters(parameters);
			shapes.add(saturnDecisionNode);

		}
		return shapes;

	}

}
